//创建一个循环列表 利用数据来建listView
import 'package:flutter/material.dart';
import 'res/listdata.dart';

//循环数据创建列表方法一
class DynamicList01 extends StatelessWidget {
//创建20个只包含标题的列表
  _getListView() {
    List listView = <Widget>[];
    for (var i = 0; i < 20; i++) {
      ListTile tile = ListTile(
        title: Text("这里是第${i + 1}行"),
      );
      listView.add(tile);
    }
    return listView.toList();
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: this._getListView(),
    );
  }
}

//循环数据创建列表方法二
class DynamicList02 extends StatelessWidget {
//创建20个只包含标题的列表
  _getListView() {
    var tempList = listData.map((e) {
      return ListTile(
        title: Text(e["title"]),
        subtitle: Text(e["author"]),
        leading: Image.network(e["imageUrl"]),
      );
    });
    return tempList.toList();
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: this._getListView(),
    );
  }
}

//循环数据创建列表方法三
class DynamicList03 extends StatelessWidget {
  var listAry = listData2.toList();
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemBuilder: (context, index) {
        return this._getItemBuilder(context, index);
      },
      itemCount: this.listAry.length,
    );
  }

  Widget _getItemBuilder(context, int index) {
    if (this.listAry[index]["title"].toString().isNotEmpty) {
      //不是空的
      return ListTile(
        title: Text(this.listAry[(index)]["title"]),
        subtitle: Text(this.listAry[index]["author"]),
        leading: Image.network(this.listAry[index]["imageUrl"]),
      );
    } else {
      return Container(
          height: 100.0,
          color: Colors.white,
          child: Center(
            child: Text(
              "这里是第${index + 1}行",
              style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold),
            ),
          ));
    }
  }
}
